import GameConfig from '../config/GameConfig.js';
import Canvas from '../utils/Canvas.js';

/**
 * 游戏结束界面类
 */
export default class GameOver {
  /**
   * 初始化游戏结束界面
   * @param {CanvasRenderingContext2D} ctx - Canvas上下文
   */
  constructor(ctx) {
      this.ctx = ctx;
      this.particles = [];
      this.createParticles();
  }

  /**
   * 绘制游戏结束界面
   * @param {number} score - 最终得分
   */
  draw(score) {
      this.ctx.save();

      // 绘制渐变背景
      const gradient = this.ctx.createLinearGradient(0, 0, 0, this.ctx.canvas.height);
      gradient.addColorStop(0, 'rgba(0, 0, 0, 0.8)');
      gradient.addColorStop(0.5, 'rgba(0, 0, 0, 0.9)');
      gradient.addColorStop(1, 'rgba(0, 0, 0, 0.8)');
      this.ctx.fillStyle = gradient;
      this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);

      // 更新和绘制粒子效果
      this.updateParticles();
      this.drawParticles();

      // 绘制中央面板
      this.drawGameOverPanel(score);

      this.ctx.restore();
  }

  /**
   * 绘制游戏结束面板
   */
  drawGameOverPanel(score) {
    const centerX = this.ctx.canvas.width / 2;
    const centerY = this.ctx.canvas.height / 2;
    const panelWidth = 400;
    const panelHeight = 320; // 增加面板高度，给元素更多空间

    // 绘制面板背景
    this.ctx.save();
    this.ctx.shadowColor = 'rgba(0, 0, 0, 0.5)';
    this.ctx.shadowBlur = 20;
    this.ctx.shadowOffsetY = 10;

    // 面板渐变背景
    const panelGradient = this.ctx.createLinearGradient(
        centerX - panelWidth/2, 
        centerY - panelHeight/2,
        centerX + panelWidth/2, 
        centerY + panelHeight/2
    );
    panelGradient.addColorStop(0, 'rgba(50, 50, 50, 0.95)');
    panelGradient.addColorStop(1, 'rgba(30, 30, 30, 0.95)');
    
    this.ctx.fillStyle = panelGradient;
    this.roundRect(
        centerX - panelWidth/2,
        centerY - panelHeight/2,
        panelWidth,
        panelHeight,
        15
    );
    this.ctx.fill();

    // 添加面板边框
    this.ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
    this.ctx.lineWidth = 2;
    this.ctx.stroke();
    this.ctx.restore();

    // 重新布局各元素位置
    const iconY = centerY - 100; // 图标位置上移
    const titleY = centerY - 30; // 标题位置上移
    const scoreY = centerY + 30; // 分数位置调整
    const promptY = centerY + 100; // 提示文字位置调整

    // 绘制游戏结束图标
    this.drawGameOverIcon(centerX, iconY);

    // 绘制文本内容
    this.ctx.textAlign = 'center';
    this.ctx.textBaseline = 'middle';

    // 游戏结束标题
    this.ctx.font = 'bold 48px "Microsoft YaHei"';
    const titleGradient = this.ctx.createLinearGradient(
        centerX - 100, titleY, 
        centerX + 100, titleY
    );
    titleGradient.addColorStop(0, '#ff6b6b');
    titleGradient.addColorStop(0.5, '#ff8787');
    titleGradient.addColorStop(1, '#ff6b6b');
    
    this.ctx.fillStyle = titleGradient;
    this.ctx.shadowColor = 'rgba(0, 0, 0, 0.5)';
    this.ctx.shadowBlur = 10;
    this.ctx.shadowOffsetY = 2;
    this.ctx.fillText('游戏结束', centerX, titleY);

    // 分数显示
    this.drawScore(centerX, scoreY, score);

    // 添加分隔线
    this.ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
    this.ctx.lineWidth = 1;
    this.ctx.beginPath();
    this.ctx.moveTo(centerX - 150, promptY - 20);
    this.ctx.lineTo(centerX + 150, promptY - 20);
    this.ctx.stroke();

    // 重新开始提示
    this.ctx.font = '24px "Microsoft YaHei"';
    this.ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
    this.ctx.shadowBlur = 5;
    this.ctx.fillText('点击任意位置重新开始', centerX, promptY);
  }

  /**
   * 绘制分数
   */
  drawScore(x, y, score) {
    this.ctx.save();

    // 分数标签
    this.ctx.font = '20px "Microsoft YaHei"';
    this.ctx.fillStyle = 'rgba(255, 255, 255, 0.6)';
    this.ctx.fillText('最终得分', x, y - 15);

    // 分数值
    this.ctx.font = 'bold 42px "Microsoft YaHei"'; // 增大分数字体
    const scoreGradient = this.ctx.createLinearGradient(x - 50, y + 20, x + 50, y + 20);
    scoreGradient.addColorStop(0, '#4CAF50');
    scoreGradient.addColorStop(0.5, '#81C784');
    scoreGradient.addColorStop(1, '#4CAF50');
    
    this.ctx.fillStyle = scoreGradient;
    this.ctx.shadowColor = 'rgba(0, 0, 0, 0.3)';
    this.ctx.shadowBlur = 8;
    this.ctx.fillText(score.toString(), x, y + 20);

    this.ctx.restore();
  }

  /**
   * 绘制游戏结束图标
   */
  drawGameOverIcon(x, y) {
    this.ctx.save();
    
    // 绘制外圈光晕
    const gradient = this.ctx.createRadialGradient(x, y, 35, x, y, 45);
    gradient.addColorStop(0, 'rgba(255, 107, 107, 0.2)');
    gradient.addColorStop(1, 'rgba(255, 107, 107, 0)');
    this.ctx.fillStyle = gradient;
    this.ctx.beginPath();
    this.ctx.arc(x, y, 45, 0, Math.PI * 2);
    this.ctx.fill();
    
    // 绘制圆形背景
    this.ctx.beginPath();
    this.ctx.arc(x, y, 35, 0, Math.PI * 2);
    this.ctx.fillStyle = 'rgba(255, 107, 107, 0.2)';
    this.ctx.fill();

    // 绘制X图标
    this.ctx.strokeStyle = '#ff6b6b';
    this.ctx.lineWidth = 4;
    this.ctx.lineCap = 'round';
    
    // 绘制X的第一条线
    this.ctx.beginPath();
    this.ctx.moveTo(x - 15, y - 15);
    this.ctx.lineTo(x + 15, y + 15);
    this.ctx.stroke();

    // 绘制X的第二条线
    this.ctx.beginPath();
    this.ctx.moveTo(x + 15, y - 15);
    this.ctx.lineTo(x - 15, y + 15);
    this.ctx.stroke();

    this.ctx.restore();
  }

  /**
   * 创建背景粒子
   */
  createParticles() {
      for (let i = 0; i < 50; i++) {
          this.particles.push({
              x: Math.random() * this.ctx.canvas.width,
              y: Math.random() * this.ctx.canvas.height,
              size: Math.random() * 3 + 1,
              speedY: Math.random() * 1 + 0.5,
              opacity: Math.random() * 0.5 + 0.1
          });
      }
  }

  /**
   * 更新粒子位置
   */
  updateParticles() {
      this.particles.forEach(particle => {
          particle.y += particle.speedY;
          if (particle.y > this.ctx.canvas.height) {
              particle.y = 0;
              particle.x = Math.random() * this.ctx.canvas.width;
          }
      });
  }

  /**
   * 绘制粒子
   */
  drawParticles() {
      this.particles.forEach(particle => {
          this.ctx.fillStyle = `rgba(255, 255, 255, ${particle.opacity})`;
          this.ctx.beginPath();
          this.ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
          this.ctx.fill();
      });
  }

  /**
   * 绘制圆角矩形
   */
  roundRect(x, y, width, height, radius) {
      this.ctx.beginPath();
      this.ctx.moveTo(x + radius, y);
      this.ctx.arcTo(x + width, y, x + width, y + height, radius);
      this.ctx.arcTo(x + width, y + height, x, y + height, radius);
      this.ctx.arcTo(x, y + height, x, y, radius);
      this.ctx.arcTo(x, y, x + width, y, radius);
      this.ctx.closePath();
  }
}